package settings.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import settings.json.OrderTableJson;
import settings.service.TableManager;

import common.action.BaseAction;


public class OrderTableAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	private ObjectMapper jsonMapper;
	private TableManager tableManager;

	public ObjectMapper getJsonMapper() {
		return jsonMapper;
	}

	public void setJsonMapper(ObjectMapper jsonMapper) {
		this.jsonMapper = jsonMapper;
	}

	public String execute(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		OrderTableJson orderTableJson = new OrderTableJson();
		try {
			orderTableJson = jsonMapper.readValue(request.getInputStream(), OrderTableJson.class);
		} 
		catch (JsonParseException e1) {
		
			e1.printStackTrace();
		} 
		catch (JsonMappingException e1) {
	
			e1.printStackTrace();
		} 
		catch (IOException e1) {
		
			e1.printStackTrace();
		}
		 
		List<Integer> listTableId = orderTableJson.getTableIds();
		tableManager.oderTableByIds(listTableId);
		Map<String, String> resMap = new HashMap<String, String>();
		resMap.put("status", "ok");
		String res = null;
		try
		{
			res = jsonMapper.writeValueAsString(resMap);
		}
		catch (RuntimeException e)
		{
			
		} catch (JsonGenerationException e) {
			
			e.printStackTrace();
		} catch (JsonMappingException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		try
		{
			this.sendClientJSON(res);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		return null;
	}

	public TableManager getTableManager() {
		return tableManager;
	}

	public void setTableManager(TableManager tableManager) {
		this.tableManager = tableManager;
	}

}
